﻿using PublicLib.Def;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace PublicLib.Lib.Security
{
    public static class AesSecurity
    {
        public static string Encrypt(string toEncrypt, string key)
        {
            return Encrypt(PublicDef.GetByte(toEncrypt), key);
        }
        public static string Encrypt(byte[] toEncryptArray, string key)
        {
            return Encrypt(toEncryptArray, PublicDef.GetByte(key));
        }
        public static string Encrypt(byte[] toEncryptArray, byte[] keyArray)
        {
            keyArray = MakeLenth(keyArray, 16);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch
            {
                return string.Empty;
            }
        }

        private static byte[] MakeLenth(byte[] keyArray, int lenth)
        {
            byte[] buffer = new byte[16] { 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 };
            if (keyArray.Length < lenth)
            {
                keyArray.CopyTo(buffer, 0);
            }
            else if (keyArray.Length > lenth)
            {
                Array.Copy(keyArray, buffer, 16);
            }
            return buffer;
        }

        public static string Decrypt(string toDecrypt, string key)
        {
            try
            {
                return Decrypt(Convert.FromBase64String(toDecrypt), key);
            }
            catch
            {
                return string.Empty;
            }
        }
        public static string Decrypt(byte[] toEncryptArray, string key)
        {
            return Decrypt(toEncryptArray, PublicDef.GetByte(key));
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="password"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Decrypt(byte[] toEncryptArray, byte[] keyArray)
        {
            keyArray = MakeLenth(keyArray, 16);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return PublicDef.GetString(resultArray);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}